﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Rapid
{
    static class PatientView
    {
        static RapidDataDataContext db = new RapidDataDataContext();

        public static string Firstname = string.Empty;
        public static string Id = string.Empty;
        public static string Address = string.Empty;
        public static string Note = string.Empty;

        public static void SetPatientInfo(string paramID, string paramFirstname, string paramAddress, string paramNote)
        {
            Id = paramID;
            Firstname = paramFirstname;
            Address = paramAddress;
            Note = paramNote;
        }

        public static void Reset()
        {
            Firstname = string.Empty;
            Id = string.Empty;
            Address = string.Empty;
            Note = string.Empty;
        }

        public static bool IsExist()
        {
            return db.Patients.Count(p => p.Id == Int32.Parse(PatientView.Id)) > 0 ? true : false;
        }

        public static bool Add()
        {
            //if (PatientView.IsExist())
            //{
            //    MessageBox.Show("Patient id already exists, please check!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return false;
            //}
            //else
            //{
                Patient patient = new Patient
                {
                    PatientID = Int32.Parse(PatientView.Id),
                    Firstname = PatientView.Firstname,
                    Address = PatientView.Address,
                    TestDate = DateTime.Now,
                    Note = PatientView.Note
                };
                db.Patients.InsertOnSubmit(patient);

                db.SubmitChanges();
                return true;
            //}
        }

        public static string getName()
        {
            if (db.Patients.Count() > 0)
            {
                //var patient = db.Patients.OrderByDescending(p => p.Id).First();
                //return patient.Firstname + " " + patient.Lastname;
                return db.Patients.OrderByDescending(p => p.Id).First().Firstname;
            }
            return "";
        }

        public static string GetTestDate()
        {
            var testDate = DateTime.Now;
            if (db.Patients.Count() > 0)
            {
                testDate = db.Patients.OrderByDescending(p => p.Id).First().TestDate;
            }
            return testDate.ToString("MMMM") + " " + testDate.Day + ", " + testDate.Year;
        }

        public static string getPatientID()
        {
            if (db.Patients.Count() > 0)
            {
                var patient = db.Patients.OrderByDescending(p => p.Id).First();

                var len = patient.Id.ToString().Length;

                string patientID = string.Empty;

                for (int i = 1; i <= 8 - len; i++)
                {
                    patientID += "0";
                }

                return patientID + patient.Id.ToString();
            }
            return "Please add patient infomation";
        }
    }
}
